
package com.sausageking.server.dao;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.Embedded;
import javax.persistence.Transient;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.NotFoundException;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.googlecode.objectify.util.DAOBase;
import com.sausageking.shared.Checkin;
import com.sausageking.shared.Counter;
import com.sausageking.shared.Deal;
import com.sausageking.shared.LoginToken;
import com.sausageking.shared.Merchant;
import com.sausageking.shared.Offer;
import com.sausageking.shared.Redemption;
import com.sausageking.shared.Consumer;

public class GenericDao<T> extends DAOBase {

  static final int BAD_MODIFIERS = Modifier.FINAL | Modifier.STATIC
      | Modifier.TRANSIENT;

  static {
    ObjectifyService.register(Checkin.class);
    ObjectifyService.register(Consumer.class);
    ObjectifyService.register(Counter.class);
    ObjectifyService.register(Deal.class);
    ObjectifyService.register(LoginToken.class);
    ObjectifyService.register(Merchant.class);
    ObjectifyService.register(Offer.class);
    ObjectifyService.register(Redemption.class);
  }

  protected Class<T> clazz;

  public GenericDao(Class<T> clazz) {
    this.clazz = clazz;
  }

  public Key<T> put(T entity) {
    return ofy().put(entity);
  }

  public Map<Key<T>, T> putAll(Iterable<T> entities) {
    return ofy().put(entities);
  }

  public void delete(T entity) {
    ofy().delete(entity);
  }

  public void deleteKey(Key<T> entityKey) {
    ofy().delete(entityKey);
  }

  public void deleteAll(Iterable<T> entities) {
    ofy().delete(entities);
  }

  public void deleteKeys(Iterable<Key<T>> keys) {
    ofy().delete(keys);
  }

  // TODO(haibozhao): change to throw {@code NotFoundException}.
  public T get(Long id) throws EntityNotFoundException {
    return ofy().get(this.clazz, id);
  }

  public T get(String id) throws NotFoundException {
    return ofy().get(this.clazz, id);
  }

  public T get(Key<T> key) throws NotFoundException {
    return ofy().get(key);
  }

  /**
   * Convenience method to get all objects matching a single property
   * 
   * @param propName
   * @param propValue
   * @return T matching Object
   */
  public T getByProperty(String propName, Object propValue) {
    Query<T> q = ofy().query(clazz);
    q.filter(propName, propValue);
    return q.get();
  }

  public List<T> listByProperty(String propName, Object propValue) {
    Query<T> q = ofy().query(clazz);
    q.filter(propName, propValue);
    return q.list();
  }

  public List<Key<T>> listKeysByProperty(String propName, Object propValue) {
    Query<T> q = ofy().query(clazz);
    q.filter(propName, propValue);
    return asKeyList(q.fetchKeys());
  }

  public T getByExample(T exampleObj) {
    Query<T> queryByExample = buildQueryByExample(exampleObj);
    Iterable<T> iterableResults = queryByExample.list();
    Iterator<T> i = iterableResults.iterator();
    T obj = i.next();
    if (i.hasNext())
      throw new RuntimeException("Too many results");
    return obj;
  }

  public List<T> listByExample(T exampleObj) {
    Query<T> queryByExample = buildQueryByExample(exampleObj);
    return queryByExample.list();
  }

  private List<Key<T>> asKeyList(Iterable<Key<T>> iterableKeys) {
    ArrayList<Key<T>> keys = new ArrayList<Key<T>>();
    for (Key<T> key : iterableKeys) {
      keys.add(key);
    }
    return keys;
  }

  private Query<T> buildQueryByExample(T exampleObj) {
    Query<T> q = ofy().query(clazz);

    // Add all non-null properties to query filter
    for (Field field : clazz.getDeclaredFields()) {
      // Ignore transient, embedded, array, and collection properties
      if (field.isAnnotationPresent(Transient.class)
          || (field.isAnnotationPresent(Embedded.class))
          || (field.getType().isArray())
          || (Collection.class.isAssignableFrom(field.getType()))
          || ((field.getModifiers() & BAD_MODIFIERS) != 0))
        continue;

      field.setAccessible(true);

      Object value;
      try {
        value = field.get(exampleObj);
      } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
      } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
      }
      if (value != null) {
        q.filter(field.getName(), value);
      }
    }
    return q;
  }
}
